﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ZDSoft.UPMS.Model.BusinessPart;
using ZDSoft.UPMS.Manager.BusinessPart;
using ZDSoft.UPMS.Service.BusinessPart;
using ZDSoft.UPMS.Model.SystemPart;
using ZDSoft.UPMS.Manager.SystemPart;
using NHibernate.Criterion;

namespace ZDSoft.UPMS.Component.BusinessPart
{
    public class SituationComponent : ComponentBase<Situation, SituationManager>, ISituationService
    {
        SituationManager situationManager = new SituationManager();
        CourseManager c = new CourseManager();
        UserManager um = new UserManager();
        SituationTypeManager situationTypeManager = new SituationTypeManager();
        SituationTypeComponent situationTypeComponent = new SituationTypeComponent();
        UserCourseSituationManager us = new UserCourseSituationManager();
        OrganizationManager orgManager = new OrganizationManager();
        TermManager term = new TermManager();
        public void Create(string selectMsg, string ddlCourse, string inputTime, string ddlOrganization, string txtRemark, string txtUserNumber, string txtTypeID, int teacherId)
        {
            IList<string> selectMsgList = selectMsg.Split(',').ToList<string>();
            selectMsgList.RemoveAt(selectMsgList.Count - 1);
            IList<string> txtRemarkList = txtRemark.Split(',').ToList<string>();
            txtRemarkList.RemoveAt(txtRemarkList.Count - 1);
            IList<string> txtUserNumberList = txtUserNumber.Split(',').ToList<string>();
            txtUserNumberList.RemoveAt(txtUserNumberList.Count - 1);
            IList<string> txtTypeIDList = txtTypeID.Split(',').ToList<string>();
            txtTypeIDList.RemoveAt(txtTypeIDList.Count - 1);

            Situation si = null;
            UserCourseSituation ucs = null;

            ///创建UserCourseSituation信息
            ucs = new UserCourseSituation();
            ucs.Course = c.FindByID(int.Parse(ddlCourse));
            ucs.Date = DateTime.Parse(inputTime);
            ucs.User = um.FindByID(teacherId);
            us.Create(ucs);

            ///循环selectMsgList，创建Situation信息
            for (int i = 0; i < selectMsgList.Count; i++)
            {
                si = new Situation();
                si.SelectCount = int.Parse(selectMsgList[i]);
                si.IsActive = true;

                si.Remark = txtRemarkList[i / txtTypeIDList.Count];


                si.SituationType = situationTypeManager.FindByID(int.Parse(txtTypeIDList[i % txtTypeIDList.Count]));
                si.userCourseSituation = ucs;
                si.Student = um.FindByID(int.Parse(txtUserNumberList[i / txtTypeIDList.Count]));
                base.Create(si);
            }
        }

        public List<int> FindAllSituation()
        {
            IList<Situation> siList = situationManager.FindAll();
            IList<SituationType> siTypeList = situationTypeManager.FindAll();
            List<int> num = new List<int>();
            int counts = 0;
            for (int i = 0; i < siTypeList.Count; i++)
            {
                for (int j = 0; j < siList.Count; j++)
                {
                    if (siTypeList[i].Name == siList[j].SituationType.Name)
                    {
                        counts += siList[j].SelectCount;
                    }
                }
                num.Add(counts);
            }
            return num;

        }

        /// <summary>
        /// 查找组织机构
        /// </summary>
        /// <returns>组织机构不为父类的数据</returns>
        public IList<Organization> FindOrganization()
        {
            IList<Organization> organizationList = orgManager.FindAll();
            FindLeafeNode(organizationList.Where(o => o.SubOrganization == null).First());
            return leafNode;
        }

        IList<Organization> leafNode = new List<Organization>();
        private void FindLeafeNode(Organization org)
        {
            if (org.Organizations == null || org.Organizations.Count == 0)
            {
                leafNode.Add(org);
            }
            else
            {
                foreach (Organization o in org.Organizations)
                {
                    FindLeafeNode(o);
                }
            }
        }

        public IList<int> FindUserCourseSituationIDByCourseID(int id)
        {
            IList<int> uscIDList = new List<int>();
            IList<UserCourseSituation> ucsList = null;
            IList<ICriterion> conditions = new List<ICriterion>();
            conditions.Add(Expression.Eq("Course.ID", id));
            ucsList = us.Find(conditions);
            for (int i = 0; i < ucsList.Count; i++)
            {
                uscIDList.Add(ucsList[i].ID);
            }
            return uscIDList;
        }
        public IList<Situation> FindEqualTypeAndStudent(int situationTypeID, int stuID)
        {
            IList<Situation> siList = situationManager.FindAll();
            IList<Situation> situationList = new List<Situation>();
            for (int i = 0; i < siList.Count; i++)
            {
                if (siList[i].SituationType.Parent.ID == situationTypeID && siList[i].Student.UserNumber == stuID.ToString())
                {
                    situationList.Add(siList[i]);
                }
            }
            return situationList;
        }
        public IList<Situation> FindEqualConditionsSituationList(string ddlterms,int courseID, int situationTypeID, int stuID)
        {
            IList<Situation> siList = new List<Situation>();
            IList<int> uscID = FindUserCourseSituationIDByCourseID(courseID);
            IList<Situation> situationList = FindEqualTypeAndStudent(situationTypeID, stuID);
            Term t = getTerm(ddlterms);
            for (int j = 0; j < uscID.Count; j++)
            {
                for (int i = 0; i < situationList.Count; i++)
                {
                    if (uscID[j] == situationList[i].userCourseSituation.ID)
                    {
                        if (situationList[i].userCourseSituation.Date >= t.StartTime && situationList[i].userCourseSituation.Date <= t.StopTime)
                        {
                            siList.Add(situationList[i]);
                        }
                    }
                }
            }
            return siList;
        }
        public IList<Situation> FindEqualConditionsSituation(string ddlterms,string ddlSituationType, string ddlCourse, string stuNumber)
        {
            IList<Situation> siList = FindEqualConditionsSituationList(ddlterms,int.Parse(ddlCourse), int.Parse(ddlSituationType), int.Parse(stuNumber));
            return siList;
        }

        public IList<string> FindSituationByID(int id)
        {
            Situation situation = situationManager.FindByID(id);
            IList<string> siList = new List<string>();
          
                siList.Add(situation.ID.ToString());
                siList.Add(situation.Student.UserNumber);
                siList.Add(situation.Student.Name);
                siList.Add(situation.SituationType.Name);
                siList.Add(situation.SelectCount.ToString());
                siList.Add(situation.userCourseSituation.Date.ToLongDateString());
                siList.Add(situation.Remark);
            
            return siList;
        }

        public void Update(string siID, string countMsg, string remarks)
        {
            Situation sit = situationManager.FindByID(int.Parse(siID));
            sit.SelectCount = int.Parse(countMsg);
            sit.Remark = remarks;
            this.Update(sit);

        }

        public Term getTerm(string ddlterms)
        {
            IList<ICriterion> conditions = new List<ICriterion>();
            Term t = term.FindByID(int.Parse(ddlterms)) ;
            return t;
        }



    }
}


